/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kamus.manager;

import com.kamus.DB;
import com.kamus.model.PasanganKata;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PasanganKataIGMgr extends BaseMgr {

    private static PasanganKataIGMgr instance;

    public PasanganKataIGMgr() {
        connection = getConnection();
    }

    public static PasanganKataIGMgr getInstance() {
        if (instance == null) {
            instance = new PasanganKataIGMgr();
        }
        return instance;
    }

    public void insertPasKata(PasanganKata pasKata) {
        String[] arrKataInd = pasKata.getIndonesia().split(";");
        int[] idInd = new int[arrKataInd.length];
        String[] arrKataGaul = pasKata.getGaul().split(";");
        int[] idGaul = new int[arrKataGaul.length];
        for (int i = 0; i < arrKataInd.length; i++) {
            for (int j = 0; j < arrKataGaul.length; j++) {
                idInd[i] = KosakataIndMgr.getInstance().cekOrInsert(arrKataInd[i]);
                idGaul[j] = KosakataGaulMgr.getInstance().cekOrInsert(arrKataGaul[j]);
                insert(idInd[i], idGaul[j]);
            }
        }
    }

    public void insert(int idInd, int idGaul) {
        cekConnection();
        try {
            psmnt = connection.prepareStatement("insert into IND_GAUL(ID_IND,ID_GAUL) values(?,?)");
            psmnt.setInt(1, idInd);
            psmnt.setInt(2, idGaul);
            psmnt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void deleteKata(int id) {
        cekConnection();
        try {
            psmnt = connection.prepareStatement("delete from IND_GAUL where ID=?");
            psmnt.setInt(1, id);
            psmnt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public ArrayList<PasanganKata> getKatas() {
        cekConnection();
        ArrayList<PasanganKata> list = new ArrayList<PasanganKata>();
        PasanganKata kata;
        try {
            psmnt = connection.prepareStatement("select ig.id as id, ki.kosakata as ind ,kg.kosakata as gaul from ind_gaul ig,kosakata_gaul kg ,kosakata_ind ki where ig.id_ind=ki.id and ig.id_gaul=kg.id order by ind");
            rs = psmnt.executeQuery();
            while (rs.next()) {
                kata = new PasanganKata();
                kata.setId(rs.getInt("id"));
                kata.setIndonesia(rs.getString("ind"));
                kata.setGaul(rs.getString("gaul"));
                list.add(kata);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }

    public PasanganKata getKata(String id) {
        cekConnection();
        PasanganKata pKata = null;
        try {
            psmnt = connection.prepareStatement("select ig.id as id, ki.kosakata as ind ,kg.kosakata as gaul from ind_gaul ig,kosakata_gaul kg ,kosakata_ind ki where ig.id_ind=ki.id and ig.id_gaul=kg.id order by ind and ig.id=?");
            psmnt.setString(1, id);
            rs = psmnt.executeQuery();
            while (rs.next()) {
                pKata = new PasanganKata();
                pKata.setId(rs.getInt("id"));
                pKata.setIndonesia(rs.getString("ind"));
                pKata.setGaul(rs.getString("gaul"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return pKata;
    }

    public void updateKata(PasanganKata kata) {
        cekConnection();
        int idGaul = KosakataGaulMgr.getInstance().cekOrInsert(kata.getGaul());
        int idInd = KosakataIndMgr.getInstance().cekOrInsert(kata.getIndonesia());
        try {
            psmnt = connection.prepareStatement("update IND_GAUL set ID_IND=?, ID_GAUL=? where ID= ?");
            psmnt.setInt(1, idInd);
            psmnt.setInt(2, idGaul);
            psmnt.setInt(3, kata.getId());
            psmnt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
